package models

{#import (#}
{#"reflect"#}
{#"context"#}
{#"encoding/json"#}
{#"ez/config/starter"#}
{#"ez/vend/orm"#}
{#"ez/vend/tool"#}
{#"gorm.io/gorm"#}
{#)#}


{#func (this *{{ table.name }}) GetReflect() reflect.Type {#}
{#return reflect.TypeOf({{ table.name }}{})#}
{#}#}
{#func New{{ table.name }}(context context.Context) *{{ table.name }} {#}
{#model := new({{ table.name }})#}
{#model.SetCtx(context)#}
{#model.Trigger("demo.{{ table.name }}New", model)#}
{#return model#}
{#}#}
{#func (this *{{ table.name }}) Save(configKey ...string) (tx *gorm.DB) {#}
{#tx = starter.DB().#}
{#Select({{ table.name }}EzConfig.GetCurdCreate(configKey...).Select).#}
{#Omit({{ table.name }}EzConfig.GetCurdCreate(configKey...).Omit...)#}
{#if this.Id == 0 {#}
{#this.Trigger("demo.Before{{ table.name }}Create", this)#}
{#tx.Create(this)#}
{#this.Trigger("demo.After{{ table.name }}Create", this)#}
{#} else {#}
{#this.Trigger("demo.Before{{ table.name }}Update", this)#}
{#tx.Save(this)#}
{#this.Trigger("demo.After{{ table.name }}Update", this)#}
{#}#}
{#return tx#}
{#}#}
{#func (this *{{ table.name }}) Delete() *gorm.DB {#}
{#this.Trigger("demo.{{ table.name }}Delete", this)#}
{#return starter.DB().Delete(this)#}
{#}#}
{#func (this *{{ table.name }}) Read(configKey ...string) *gorm.DB {#}
{#return starter.DB().#}
{#Select({{ table.name }}EzConfig.GetCurdSelect(configKey...).Select).#}
{#Omit({{ table.name }}EzConfig.GetCurdSelect(configKey...).Omit...).Where(this).First(this)#}
{#}#}
{#func (this *{{ table.name }}) CreateFromObject(jsonData interface{}) *{{ table.name }} {#}
{#tableByte, err := json.Marshal(jsonData)#}
{#if err != nil {#}
{#println(err.Error())#}
{#}#}
{#err = json.Unmarshal(tableByte, this)#}
{#if err != nil {#}
{#println(err.Error())#}
{#}#}
{#return this#}
{#}#}
{#func (this *{{ table.name }}) CreateFromString(jsonString string) *{{ table.name }} {#}
{#_ = json.Unmarshal([]byte(jsonString), this)#}
{#return this#}
{#}#}
{#func (this *{{ table.name }}) ToString() string {#}
{#ret, _ := json.Marshal(this)#}
{#return string(ret)#}
{#}#}
{#func (this *{{ table.name }}) FillList(name string, ids []int) orm.Ormer #}{#}#}
{#    o := orm.NewOrm()#}
{#    if this.Id == 0 #}{#}#}
{#        this.Save()#}
{#    }#}
{#    //__M2M_FILL__#}
{#{% for f in fields %}#}
{#{% if f.EntityType == "M2M" %}#}
{#    if name == "{{ f.Name }}" #}{#}#}
{#        m2mQs := o.QueryM2M(this, "{{ f.Name }}")#}
{#        m2mQs.Clear()#}
{#        for _, id := range ids #}{#}#}
{#            m2mQs.Add({{ f.Src }}{Id: id})#}
{#        }#}
{#    }#}
{#{% endif %}#}
{#{% endfor %}#}
{#    return o#}
{#}#}
{#func (this *{{ table.name }}) ToArray(fields string) map[string]interface{} {#}
{#    var data = make(map[string]interface{})#}
{#    if fields== "" || ez.HasSameWord(fields,"id") {#}
{#        data["id"] = this.Id#}
{#    }#}
{#    //__TO_ARRAY_FILL__#}
{#{% for f in fields %}#}
{#{% if f.EntityType in ["M2M","O2M"] %}#}
{#    if fields== "" ||  ez.HasSameWord(fields,"{{ f.Name }}") #}{#}#}
{#        qb:=this.GetQueryBuilder()#}
{#        this.GetOrm().LoadRelated(this,"{{ f.Name }}",hints.OrderBy(qb.GetOrderString()))#}
{#        dataMap := make([]interface{},len(this.{{ f.Name | capitalize }}))#}
{#        for index, item := range this.{{ f.Name | capitalize }} #}{#}#}
{#            dataMap[index] = item#}
{#        }#}
{#        data["{{ f.Name }}"] = dataMap#}
{#    }#}
{#{% elseif f.EntityType in ["O2O","M2O"] %}#}
{#    if fields== "" ||  ez.HasSameWord(fields, "{{ f.Name }}") {#}
{#        if this.{{ f.Name | capitalize }} !=nil {#}
{#            this.{{ f.Name | capitalize }}.Read()#}
{#        }#}
{#        data["{{ f.Name }}"] = this.{{ f.Name | capitalize }}#}
{#    }#}
{#{% else %}#}
{#    if fields== "" ||  ez.HasSameWord(fields, "{{ f.Name }}") {#}
{#        data["{{ f.Name }}"] = this.{{ f.Name | capitalize }}#}
{#    }#}
{#{% endif %}#}
{#{% endfor %}#}
{#    return data#}
{#}#}
{#func (this *{{ table.name }}) ToArrayEdit() map[string]interface{} {#}
{#    var data = make(map[string]interface{})#}
{#    data["id"] = this.Id#}
{#    //__EDIT_FILL__#}
{#{% for f in fields %}#}
{#{% if f.EntityType in ["M2M","O2M"] %}#}

{#{% elseif f.EntityType in ["O2O","M2O"] %}#}
{#{% elseif f.EntityType == "JSON" %}#}
{#{% else %}#}
{#    data["{{ f.Name }}"] = this.{{ f.Name | capitalize }}#}
{#{% endif %}#}
{#{% endfor %}#}
{#    return data#}
{#}#}
{#func (this *{{ table.name }}) Scan(fields string,depth int,parent string) map[string]interface{} {#}
{#    var data = make(map[string]interface{})#}
{#    if fields== "" || ez.HasSameWord(fields, parent + "id") {#}
{#        data["id"] = this.Id#}
{#    }#}
{#    //__SCAN_FILL__#}
{#{% for f in fields %}#}
{#{% if f.EntityType in ["M2M","O2M"] %}#}
{#    if fields== "" || ez.HasSameWord(fields, parent + "{{ f.Name }}") {#}

{#    }#}
{#{% elseif f.EntityType in ["O2O","M2O"] %}#}
{#    if fields== "" || ez.HasSameWord(fields, parent + "{{ f.Name }}") {#}

{#    }#}
{#{% elseif f.EntityType == "JSON" %}#}
{#    var tmp{{ f.Name | capitalize }} interface{}#}
{#    if fields== "" ||  ez.HasSameWord(fields, parent + "{{ f.Name }}") {#}

{#    }#}
{#{% else %}#}
{#    if fields== "" ||  ez.HasSameWord(fields, "{{ f.Name }}") {#}
{#        data["{{ f.Name }}"] = this.{{ f.Name | capitalize }}#}
{#    }#}
{#{% endif %}#}
{#{% endfor %}#}
{#    return data#}
{#}#}

{#/***************************************#}
{#Repository#}
{#****************************************/#}
{#type {{ table.name }}Crud struct{}#}

{#func (this *{{ table.name }}Crud) Find(id int, configKey ...string) *{{ table.name }} {#}
{#model := new({{ table.name }})#}
{#model.Id = id#}
{#model.Read(configKey...)#}
{#return model#}
{#}#}
{#func (this *{{ table.name }}Crud) FindBy(where map[string]interface{}, order []string, page int, Limit int) ([]{{ table.name }}, error) {#}
{#qb := orm.NewQueryBuilder(starter.DB(), {{ table.name }}EzConfig)#}
{#qb.Prepare(where,order,page,Limit)#}
{#rst := make([]{{ table.name }}, 0)#}
{#tx := qb.Tx.Find(&rst)#}
{#if tx.Error != nil {#}
{#return nil, tx.Error#}
{#}#}
{#return rst, nil#}
{#}#}

{#func (this *{{ table.name }}Crud) FindOneBy(where map[string]interface{}, order []string) ({{ table.name }}, error) {#}
{#qb := orm.NewQueryBuilder(starter.DB(), {{ table.name }}EzConfig)#}
{#qb.Prepare(where,order,1,1)#}
{#rst := {{ table.name }}{}#}
{#tx := qb.Tx.First(&rst)#}
{#if tx.Error != nil {#}
{#return rst, tx.Error#}
{#}#}
{#return rst, nil#}
{#}#}
